package com.mopa.qzone;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.widget.ScrollView;
import android.widget.TextView;
import com.mopa.http.QZoneItem;
import com.mopa.http.QzoneUploadPic;
public class QzoneShareActivity extends Activity {
	public String mAccessToken, mOpenId;
	private QZoneLayout qLayout;
	public String mAppid=QZoneItem.APPID;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        qLayout=new QZoneLayout(this);
        setContentView(qLayout);
    }
    public void setIdAndToken(String id,String token)
    {
    	mOpenId=id;
    	mAccessToken=token;
    }
    public static final int REQUEST_PICK_PICTURE = 1001;
	public void showMessage(String title, String msg) {
		Dialog dialog = new Dialog(QzoneShareActivity.this);
		ScrollView rootView = new ScrollView(QzoneShareActivity.this);
		TextView view = new TextView(QzoneShareActivity.this);
		view.setText(msg);
		rootView.addView(view);
		dialog.setContentView(rootView);
		dialog.setTitle(title);
		dialog.show();
	}
	
	public boolean satisfyConditions() {
		return 	mAccessToken != null && 
		mAppid != null && 
		mOpenId != null && 
		!mAccessToken.equals("") && 
		!mAppid.equals("") && 
		!mOpenId.equals("");
	}
	
	public static final int PROGRESS = 0;
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case PROGRESS:
			dialog = new ProgressDialog(this);
			((ProgressDialog)dialog).setMessage("Loading..");
			break;
		}
		return dialog;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_PICK_PICTURE: {
			if (data != null) {
				String picPath=getPickPhotoPath(data);
				if (picPath != null) {
					Bundle baseParams=new Bundle();
					baseParams.putString("picPath", picPath);
					baseParams.putString("content", "___Mopa");
					baseParams.putString("accessToken",mAccessToken);
					baseParams.putString("appId", QZoneItem.APPID);
					baseParams.putString("openId",mOpenId);
					new QzoneUploadPic().uploadPic(new Handler(), baseParams, new QzoneUploadPic.OnUploadListener() {
						@Override
						public void onStart() {
							// TODO Auto-generated method stub
							showDialog(PROGRESS);
						}
						
						@Override
						public void onSuncces(String sMsg) {
							// TODO Auto-generated method stub
							dismissDialog(PROGRESS);
							showMessage("发送成功:", sMsg);
						}
						
						@Override
						public void onFail(int ret, String fMsg) {
							// TODO Auto-generated method stub
							dismissDialog(PROGRESS);
							showMessage("发送失败:", "ret:"+ret+"\n"+"fMsg:"+fMsg);
						}

					});
				}
			}
		}
		}
	}
	
	private String getPickPhotoPath(Intent data) { 
		String path=null;
		    Uri originalUri = data.getData();  
		    String[] proj = {MediaStore.Images.Media.DATA};  
		    Cursor cursor = (QzoneShareActivity.this).managedQuery(originalUri, proj, null, null, null);   
		    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
		    cursor.moveToFirst();  
		    path= cursor.getString(column_index);
		return path;  
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}